package cn.lili.modules.promotion.serviceimpl;

import cn.lili.common.exception.ServiceException;
import cn.lili.common.security.enums.SceneEnums;
import cn.lili.common.utils.StringUtils;
import cn.lili.common.vo.PageVO;
import cn.lili.modules.promotion.entity.dos.Ticket;
import cn.lili.modules.promotion.entity.dos.TicketOrder;
import cn.lili.modules.promotion.entity.enums.TicketOrderStatusEnum;
import cn.lili.modules.promotion.entity.enums.TicketSaleStateEnum;
import cn.lili.modules.promotion.mapper.TicketOrderMapper;
import cn.lili.modules.promotion.service.TicketOrderService;
import cn.lili.modules.promotion.service.TicketService;
import cn.lili.mybatis.util.PageUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@RequiredArgsConstructor
public class TicketOrderServiceImpl extends ServiceImpl<TicketOrderMapper, TicketOrder>
        implements TicketOrderService {

    private final TicketService ticketService;

    /**
     * 分页查询订单列表
     * @param ticketOrder
     * @param page
     * @return
     */
    @Override
    public Page<TicketOrder> getByPage (TicketOrder ticketOrder, PageVO page) {
        LambdaQueryWrapper<TicketOrder> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotEmpty(ticketOrder.getOrderNo()), TicketOrder::getOrderNo, ticketOrder.getOrderNo());
        queryWrapper.eq(StringUtils.isNotEmpty(ticketOrder.getUserName()), TicketOrder::getUserName, ticketOrder.getUserName());
        queryWrapper.eq(StringUtils.isNotEmpty(ticketOrder.getTicket()), TicketOrder::getTicket, ticketOrder.getTicket());
        queryWrapper.eq(StringUtils.isNotEmpty(ticketOrder.getOrderStatus()), TicketOrder::getOrderStatus, ticketOrder.getOrderStatus());
        queryWrapper.between(null != ticketOrder.getStartTime() && null != ticketOrder.getEndTime(), TicketOrder::getCreateTime, ticketOrder.getStartTime(), ticketOrder.getEndTime());
        queryWrapper.eq(StringUtils.isNotEmpty(ticketOrder.getSeat()), TicketOrder::getSeat, ticketOrder.getSeat());
        queryWrapper.orderByDesc(TicketOrder::getPlaceTime);
        return this.baseMapper.selectPage(PageUtil.initPage(page), queryWrapper);
    }

    /**
     * 创建订单
     * @param ticketOrder
     */
    @Override
    public void createOrder (TicketOrder ticketOrder) {
        this.save(ticketOrder);

        Ticket ticket = ticketService.getById(ticketOrder.getTicketId());
        if (null == ticket) {
            throw new ServiceException("门票信息异常");
        }
        ticket.setOrderNo(ticketOrder.getOrderNo());
        ticket.setBuyerId(ticketOrder.getUserId());
        ticket.setBuyerName(ticketOrder.getUserName());
        ticket.setSaleState(TicketSaleStateEnum.LOCKSALE.name());
        ticket.setScene(SceneEnums.MEMBER.name());
        ticketService.updateById(ticket);
    }

    /**
     * 修改订单状态
     * @param transactionSn 外部订单号
     * @param orderStatus 订单状态
     */
    @Override
    public void updateOrderStatus (String transactionSn, String orderStatus) {
        LambdaQueryWrapper<TicketOrder> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TicketOrder::getTransactionSn, transactionSn);
        List<TicketOrder> ticketOrderList = this.list(queryWrapper);
        if (!ticketOrderList.isEmpty()) {
            for (TicketOrder ticketOrder : ticketOrderList) {
                ticketOrder.setOrderStatus(orderStatus);
                this.updateById(ticketOrder);

                if (TicketOrderStatusEnum.CANCELLED.name().equals(orderStatus) ||
                        TicketOrderStatusEnum.REFUND.name().equals(orderStatus) ||
                        TicketOrderStatusEnum.RETURN_GOODS.name().equals(orderStatus)) {
                    // 释放门票
                    LambdaUpdateWrapper<Ticket> updateWrapper = new LambdaUpdateWrapper<>();
                    updateWrapper.eq(Ticket::getId, ticketOrder.getTicketId());
                    updateWrapper.set(Ticket::getSaleState, TicketSaleStateEnum.ONSALE);
                    updateWrapper.set(Ticket::getBuyerId, null);
                    updateWrapper.set(Ticket::getBuyerName, null);
                    updateWrapper.set(Ticket::getOrderNo, null);
                    ticketService.update(updateWrapper);
                }else if (TicketOrderStatusEnum.PAID.name().equals(orderStatus)){
                    LambdaUpdateWrapper<Ticket> updateWrapper = new LambdaUpdateWrapper<>();
                    updateWrapper.eq(Ticket::getId, ticketOrder.getTicketId());
                    updateWrapper.set(Ticket::getSaleState, TicketSaleStateEnum.OFFSALE);
                    ticketService.update(updateWrapper);
                }

            }
        }
    }

    /**
     * 根据商城订单号查询门票订单
     * @param transactionSn
     * @return
     */
    @Override
    public List<TicketOrder> getListByTransactionSn(String transactionSn) {
        LambdaQueryWrapper<TicketOrder> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TicketOrder::getTransactionSn, transactionSn);
        return this.list(queryWrapper);
    }
}
